﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using S.Common.Enums.Parturition;
using S.Common.helper;
using S.Parturition.Read.API.Application.Command;
using Yitter.IdGenerator;

namespace S.Parturition.Read.API.Controllers
{
    /// <summary>
    /// 种母分娩读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ParturitionController : ControllerBase
    {

        private readonly IMediator mediator;

        private readonly IConfiguration configuration;

        private readonly IHttpContextAccessor httpContextAccessor;

        public ParturitionController(IMediator mediator, IConfiguration configuration, IHttpContextAccessor httpContextAccessor)
        {
            this.mediator = mediator;
            this.configuration = configuration;
            this.httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 显示种母分娩信息
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> GetParturitionList(GetParturitionListCommand comm)
        {
            var list = await mediator.Send(comm);
            list.StatusCode = 200;
            list.Message = "success";
            list.Timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
            return Ok(list);
        }

        /// <summary>
        /// 将分娩难易枚举转为数组
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult DifficultyDeliveryEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<DifficultyDeliveryEnum>())
            {
                list.Add(new
                {
                    label = item.ToString(),
                    value = item
                });
            }
            return Ok(list);
        }

        /// <summary>
        /// 将肥羊批次转为数组
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult FatSheepEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<FatSheepEnum>())
            {
                list.Add(new
                {
                    label = item.ToString(),
                    value = item
                });
            }
            return Ok(list);
        }

        /// <summary>
        /// 将通用枚举转为数组
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GenericEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<Genericlist>())
            {
                list.Add(new
                {
                    label = item.ToString(),
                    value = item
                });
            }
            return Ok(list);
        }

        /// <summary>
        /// 将出生状态转为数组
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult BirthStatusEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<BirthStatus>())
            {
                list.Add(new
                {
                    label = item.ToString(),
                    value = item
                });
            }
            return Ok(list);
        }

        /// <summary>
        /// 雪花Id显示
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetYitIdHelper()
        {
            return Ok(YitIdHelper.NextId());
        }

        /// <summary>
        /// 获取到已经分娩的耳号
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetParturitionEarTag()
        {
            return Ok(await mediator.Send(new GetEarTagListCommand() { }));
        }

        /// <summary>
        /// 根据种公种母查询羔羊信息
        /// </summary>
        /// <param name="comm"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> GetLamb(GetLambRegistrationCommand comm)
        {
            return Ok(await mediator.Send(comm));
        }

        /// <summary>
        /// 获取到所有种母和羔羊信息
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetLambList()
        {
            return Ok(await mediator.Send(new GetLambListCommand() { }));
        }

        /// <summary>
        /// 获取到羔羊耳号
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetLambEarTag()
        {
            return Ok(await mediator.Send(new GetLambEarTagCommand() { }));
        }


        /// <summary>
        /// 根据耳号获取到羔羊信息
        /// </summary>
        /// <param name="earTag"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> EditLamb(string earTag)
        {
            return Ok(await mediator.Send(new GetLambByIdCommand() { EarTag = earTag }));
        }

        /// <summary>
        /// 根据耳号获取种母最新信息
        /// </summary>
        /// <param name="EarTag"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetLambParturition(string EarTag)
        {
            return Ok(await mediator.Send(new GetParturitionByLambCommand() { EarTag = EarTag }));
        }

        /// <summary>
        /// 跨服务根据种母耳号获取种公信息
        /// </summary>
        /// <param name="EarTag"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetFatherInfo(string EarTag)
        {
            var jsonString = HttpClientHelper.GetResponse($"{configuration.GetValue<string>("Gateway")}/ReadBreed/api/ReadMaternalMating/ShowRamEar?ear=" + EarTag);
            Console.WriteLine(jsonString);
            var response = JsonConvert.DeserializeObject<MaternalMating>(jsonString);
            return Ok(response);
        }

        /// <summary>
        /// 根据id获取到种母信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetParturitionId(int id)
        {
            return Ok(await mediator.Send(new GetParturitionByIdCommand() { ParturitionId = id }));
        }



        /// <summary>
        /// 测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult TestGet()
        {
            var cfRt = configuration.GetValue<string>("consullp");//读取Consul路由
            return Ok(cfRt);
        }

        public class MaternalMating
        {
            /// <summary>
            /// 可视耳号
            /// </summary>
            public string EarTag { get; set; }

            /// <summary>
            /// 种公品种
            /// </summary>
            public string? SeedVariety { get; set; }

            /// <summary>
            /// 配种公羊
            /// </summary>
            public string? BreedingRam { get; set; }

            /// <summary>
            /// 种母品种
            /// </summary>
            public string? SeedMotherVariety { get; set; }
        }


    }
}
